layui.use(['form','layer','table'],function(){
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        table = layui.table;
    //添加验证规则
    form.verify({
        searchNotNull : function(value){
            if(value.trim() ==""){
                return "请输入查找内容";
            }
        }
    });

    //权限列表初始化
    var tableIns = table.render({
        elem: '#powerList',
        url : 'json/powerList.json',//TODO
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'id', title: '权限编号', minWidth:100, align:"center"},
            {field: 'name', title: '权限名称', minWidth:100, align:"center"},
            {field: 'url', title: '权限码', minWidth:200, align:'center'},
            {field: 'icon', title: '权限图标', minWidth:200, align:'center',templet:function(obj){
                return "<i class='layui-icon'>"+obj.icon+"</i>"
            }},
            {title: '操作', minWidth:175, templet:'#powerListBar',fixed:"right",align:"center"}
        ]]
    });
    //搜索
    form.on("submit(search)",function () {
        layer.msg($(".searchVal").val());//TODO
    });

    //添加或修改权限公用方法
    function edit(val){
        var temp="添加权限";
        if(val!=null)
            temp="修改权限";
            layer.open({
            title : temp,
            type : 2,
            content : "page/power/add.jsp",
            skin:'layui-layer-molv',
            area: ['600px', '400px'],
            btn:['提交'],
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(val){
                    body.find(".name").val(val.name);
                    body.find(".id").val(val.id);
                    body.find(".url").val(val.url);
                    body.find(".icon").val(val.icon);
                    form.render();
                }
            },
            yes: function (index) {//提交按钮回调方法
                layer.msg("提交中...");//TODO
                layer.close(index);
            }
        })

    };
    //添加权限
    $(".add_btn").click(function () {
        edit();
    });
    //分配权限
    $(".distribution_btn").click(function(){
        var index=layer.open({
            title : "分配权限",
            type : 2,
            content : "page/power/distribution.jsp",
            skin:'layui-layer-molv',
            success : function(layero, index){
                setTimeout(function(){
                    layer.tips('点击此处返回权限列表', '.layui-layer-setwin .layui-layer-close', {tips: 3});
                },500)
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        });
    });
    //(分配权限再次)加载角色列表
    table.render({
        elem : "#roleList",
        url : "json/roleList.json",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'id', title: '编号', align:"center"},
            {field: 'name', title: '角色名称',align:"center"},
            {field: 'roleType', title: '角色类型',align:"center"},
            {field: 'status', title: '状态', align:"center",templet : function(obj){
                    return (obj.status==0?"启用":"禁用");
                }}
        ]]
    });
    //监控选择的角色列表
    table.on('radio(roleList)',function(obj){
        layer.msg(obj.data.id);
    });
    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('powerList'),
            data = checkStatus.data,
            id = [];
        if(data.length > 0) {
            for (var i in data) {
                id.push(data[i].id);
            }
            layer.confirm("确定删除选中的["+id.length+"]个权限？", {icon: 3, title: '提示信息'}, function (index) {
                tableIns.reload();//TODO
                layer.close(index);
            });
        }else{
            layer.msg("请选择需要删除的权限");
        }
    });
    //列表操作
    table.on('tool(powerList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑权限
            edit(data);
        }
        if (layEvent==='del') {//删除权限
            layer.confirm("确定删除["+data.name+"]权限吗？",{icon:3, title:'提示信息'},function(index){
                tableIns.reload();//TODO
                layer.close(index);
            });
        }
    });


})